package com.tensquare.article.service;

import com.tensquare.article.dao.ArticleDao;
import com.tensquare.article.pojo.Article;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import util.IdWorker;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author:zimmor
 * @Date: 2021/3/1 22:07
 * @Description 文章的业务逻辑层
 * @version: 1.0.0
 */
@Service
@Transactional
public class ArticleService {
    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 点赞
     * @param id
     */
    public void thumbup(String id) {
        articleDao.updateThumbById(id);
    }

    /**
     * 审核文章
     * @param id
     */
    public void updateStateById(String id){
        articleDao.updateStateById(id);
    }

    /**
     * 分页查询
     * @param article
     * @param page
     * @param size
     * @return
     */
    public Page<Article> findSearch(Article article, int page, int size) {
        Pageable pageable = PageRequest.of(page-1, size);
        Specification<Article> specification = new Specification<Article>() {
            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List list = new ArrayList();
                if(article.getTitle()!=null && !"".equals(article.getTitle())) {
                    Predicate title = criteriaBuilder.like(root.get("title").as(String.class), "%" + article.getTitle() + "%");
                    list.add(title);
                }
                if(article.getContent()!=null && !"".equals(article.getContent())) {
                    Predicate content = criteriaBuilder.like(root.get("content").as(String.class), "%" + article.getContent() + "%");
                    list.add(content);
                }
                Predicate[] predicates = new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
        return articleDao.findAll(specification, pageable);
    }

    /**
     * 条件查询
     * @param article
     * @return
     */
    public List<Article> findSearch(Article article) {
        return articleDao.findAll(new Specification<Article>() {
            @Override
            public Predicate toPredicate(Root<Article> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List list = new ArrayList();
                if(article.getTitle()!=null && !"".equals(article.getTitle())){
                    Predicate title = criteriaBuilder.like(root.get("title").as(String.class), "%" + article.getTitle() + "%");
                    list.add(title);
                }
                if(article.getContent()!=null && !"".equals(article.getContent())) {
                    Predicate content = criteriaBuilder.like(root.get("content").as(String.class), "%" + article.getContent() + "%");
                    list.add(content);
                }
                Predicate[] predicates =new Predicate[list.size()];
                list.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        });
    }

    /**
     * 获取所有文章
     * @return
     */
    public List<Article> findAll() {
        return articleDao.findAll();
    }
    /**
     * 根据id查找文章
     * @param id
     * @return
     */
    public Article findById(String id) {
        if(redisTemplate.opsForValue().get("article_"+id) != null) {
            return (Article)redisTemplate.opsForValue().get("article_"+id);
        }
        Article article = articleDao.findById(id).get();
        redisTemplate.opsForValue().set("article_"+id, article, 10, TimeUnit.SECONDS);
        return article;
    }

    /**
     * 新增
     * @param article
     */
    public void add(Article article) {
        article.setId(idWorker.nextId()+"");
        articleDao.save(article);
    }

    /**
     * 修改
     * @param article
     */
    public void update(Article article) {
        redisTemplate.delete("article_"+article.getId());
        articleDao.save(article);
    }

    /**
     * 根据id删除文章
     * @param id
     */
    public void deleteById(String id) {
        redisTemplate.delete("article_"+id);
        articleDao.deleteById(id);
    }
}
